package com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenNeirong;
//import com.tjsinfo.tjsoa.documentationflow.tongzhifawen.vo.TTongzhifawenFujian;
//import com.tjsinfo.tjsoa.documentationflow.tongzhifawen.vo.TTongzhifawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.dao.ITXingzhengfawenNeirongDAO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.FaWenInfo;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenBeizhu;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenFujian;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TXingzhengfawenZhengwen;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.TaoHongMoBan;
import com.tjsinfo.tjsoa.documentationflow.xingzhengfawen.vo.YinZhang;
import com.tjsinfo.tjsoa.message.vo.TjsMessage;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class TXingzhengfawenNeirongBO implements ITXingzhengfawenNeirongBO	{
	private ITXingzhengfawenNeirongDAO xingzhengfawenNeirongDAO;
	private ISystemUserBo systemUserBo;

	private ISpringDao springDao;
	
	
	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public ITXingzhengfawenNeirongDAO getXingzhengfawenNeirongDAO() {
		return xingzhengfawenNeirongDAO;
	}

	public void setXingzhengfawenNeirongDAO(
			ITXingzhengfawenNeirongDAO xingzhengfawenNeirongDAO) {
		this.xingzhengfawenNeirongDAO = xingzhengfawenNeirongDAO;
	}

	public String addTXingzhengfawenneirong(
			TXingzhengfawenNeirong xingzhengfawenNeirong,String hegaorenid,String huiqian,String zhusongid,String chaosongid,List<Files> list) {
		// TODO Auto-generated method stub
		
		String id = xingzhengfawenNeirongDAO.save(xingzhengfawenNeirong);
		List<String> fenfalist = new ArrayList<String>();
		List<String> huiqianlist = new ArrayList<String>();
		String[] zhusongarray = zhusongid.split(",");
		for (int i = 0; i < zhusongarray.length; i++) {
			if (zhusongarray[i] !=null &&!"".equals(zhusongarray[i])) {
				fenfalist.add(zhusongarray[i]);
			}
		}
		if (chaosongid != null) {
			String[] chaosongarray = chaosongid.split(",");	
			for (int i = 0; i < chaosongarray.length; i++) {
				if (chaosongarray[i] !=null &&!"".equals(chaosongarray[i])) {
					fenfalist.add(chaosongarray[i]);
				}
			}
		}
		String huiqianid="1";
		if (huiqian != null) {     
			String[] huiqianarray = huiqian.split(",");	
			if (huiqianarray[0] !=null &&!"".equals(huiqianarray[0])) {
				huiqianid=huiqianarray[0];
			}
			for (int i = 1; i < huiqianarray.length; i++) {
				if (huiqianarray[i] !=null &&!"".equals(huiqianarray[i])) {
					huiqianlist.add(huiqianarray[i]);
				}
			}
		}
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		if (hegaorenid !=null && !"".equals(hegaorenid)) {
				flowid =  flowTemplate.creatProcessInstance("部门审核", hegaorenid, "行政发文");
				flowTemplate.setVariable("流程名称", "行政发文", flowid);
			
			fenfalist = systemUserBo.userlistOrdery(fenfalist);
			flowTemplate.setVariable("封发", fenfalist, flowid);
		} else  if (huiqian !=null && !"".equals(huiqian)){
			flowid =  flowTemplate.creatProcessInstance("部门主管会签", huiqianid, "行政发文","to 部门主管会签");
			flowTemplate.setVariable("流程名称", "行政发文", flowid);
			flowTemplate.setVariable("部门会签顺序组", huiqianlist, flowid);
		
			fenfalist = systemUserBo.userlistOrdery(fenfalist);
			flowTemplate.setVariable("封发", fenfalist, flowid);
		}else{
			List<TSystemUser>  systemuserlist = new ArrayList<TSystemUser>();
			
			systemuserlist = systemUserBo.selectByDutyName("办公室主任");
				
			TSystemUser systemUser = systemuserlist.get(0);
				flowid =  flowTemplate.creatProcessInstance("办公室主任复核", systemUser.getUserId().toString(), "行政发文","to 办公室主任复核");
				flowTemplate.setVariable("流程名称", "行政发文", flowid);
			
			fenfalist = systemUserBo.userlistOrdery(fenfalist);
			flowTemplate.setVariable("封发", fenfalist, flowid);
			
		}
		
		xingzhengfawenNeirong =	selectOneById(id);
		xingzhengfawenNeirong.setFlowid(flowid.intValue());
		updateTXingzhengfawenneirong(xingzhengfawenNeirong);
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TXingzhengfawenFujian xingzhengfawenFujian = new TXingzhengfawenFujian();
			xingzhengfawenFujian.setFujianAddress(file.toFilePath);
			xingzhengfawenFujian.setFujianName(file.getOldFileName());
			xingzhengfawenFujian.setFujianZhengname(file.getNewFileName());
			xingzhengfawenFujian.setXingzhengfawenNeirong(xingzhengfawenNeirong);
			xingzhengfawenNeirongDAO.saveFuJian(xingzhengfawenFujian);
			

		}
		flowTemplate.setVariable("拟稿人修改内容", xingzhengfawenNeirong.getNigaorenid().toString(), flowid);
		return id;
		
	}
	
	public String addqfTXingzhengfawenneirong(
			TXingzhengfawenNeirong xingzhengfawenNeirong,String huiqian,String zhusongid,String chaosongid,List<Files> list) {
		// TODO Auto-generated method stub
		
		String id = xingzhengfawenNeirongDAO.save(xingzhengfawenNeirong);
		List<String> fenfalist = new ArrayList<String>();
		List<String> huiqianlist = new ArrayList<String>();
		String[] zhusongarray = zhusongid.split(",");
		for (int i = 0; i < zhusongarray.length; i++) {
			if (zhusongarray[i] !=null &&!"".equals(zhusongarray[i])) {
				fenfalist.add(zhusongarray[i]);
			}
		}
		if (chaosongid != null) {
			String[] chaosongarray = chaosongid.split("、");	
			for (int i = 0; i < chaosongarray.length; i++) {
				if (chaosongarray[i] !=null &&!"".equals(chaosongarray[i])) {
					fenfalist.add(chaosongarray[i]);
				}
			}
		}
		String huiqianid="1";
		if (huiqian != null) {     
			String[] huiqianarray = huiqian.split("、");	
			if (huiqianarray[0] !=null &&!"".equals(huiqianarray[0])) {
				huiqianid=huiqianarray[0];
			}
			for (int i = 1; i < huiqianarray.length; i++) {
				if (huiqianarray[i] !=null &&!"".equals(huiqianarray[i])) {
					huiqianlist.add(huiqianarray[i]);
				}
			}
		}		
		
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		String regEx = "[\u4e00-\u9fa5]";
		Pattern pat = Pattern.compile(regEx);
		//boolean zsid=isContainChinese(zhusongid);
		boolean csid=isContainChinese(chaosongid);
		if(csid==false){
			if (fenfalist.size()!=0) {
				fenfalist = systemUserBo.userlistOrdery(fenfalist);
				flowid =  flowTemplate.creatProcessInstance("封发", fenfalist, "行政发文","to 封发");
				flowTemplate.setVariable("流程名称", "行政发文", flowid);	
				flowTemplate.setVariable("封发", fenfalist, flowid);
			}else{
				String[] zhusong = xingzhengfawenNeirong.getZhusong().split(",");	
				List<String> zhusongList = new ArrayList<String>();
				Collections.addAll(zhusongList, zhusong);
				flowid =  flowTemplate.creatProcessInstance("封发",zhusongList, "行政发文","to 封发");
				flowTemplate.setVariable("流程名称", "行政发文", flowid);	
				flowTemplate.setVariable("封发", zhusongList, flowid);
			}
		}else{
			String[] zhusong = xingzhengfawenNeirong.getZhusong().split(",");	
			List<String> zhusongList = new ArrayList<String>();
			Collections.addAll(zhusongList, zhusong);
			flowid =  flowTemplate.creatProcessInstance("封发",zhusongList, "行政发文","to 封发");
			flowTemplate.setVariable("流程名称", "行政发文", flowid);	
			flowTemplate.setVariable("封发", zhusongList, flowid);
		}
		
		xingzhengfawenNeirong =	selectOneById(id);
		xingzhengfawenNeirong.setFlowid(flowid.intValue());
		updateTXingzhengfawenneirong(xingzhengfawenNeirong);
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			String chaobao=xingzhengfawenNeirong.getChaobao();
			String miji=xingzhengfawenNeirong.getMiji();
			String fujian=xingzhengfawenNeirong.getFujian();
			boolean a=xingzhengfawenNeirong.getChaobao().contains(",");
			boolean b=xingzhengfawenNeirong.getMiji().contains(",");
			boolean c=xingzhengfawenNeirong.getFujian().contains(",");
			if (chaobao != null) {     
				if(a==true){			
					String[] chaobaoarray = xingzhengfawenNeirong.getChaobao().split(",");					
					for (int i = 1; i < chaobaoarray.length; i++) {
						if (chaobaoarray[i] !=null &&!"".equals(chaobaoarray[i])) {
							if(chaobaoarray[i].equals(file.oldFileName)){
								TXingzhengfawenZhengwen xingzhengfawenzhengwen=new TXingzhengfawenZhengwen();
								xingzhengfawenzhengwen.setzhengwenAddress(file.toFilePath);
								xingzhengfawenzhengwen.setzhengwenName(file.getOldFileName());
								xingzhengfawenzhengwen.setzhengwenZhengname(file.getNewFileName());
								xingzhengfawenzhengwen.setXingzhengfawenNeirong(xingzhengfawenNeirong);
								xingzhengfawenNeirongDAO.saveZhengWen(xingzhengfawenzhengwen);
							}
						}
					}
				}else{
					if(xingzhengfawenNeirong.getChaobao().equals(file.oldFileName)){
						TXingzhengfawenZhengwen xingzhengfawenzhengwen=new TXingzhengfawenZhengwen();
						xingzhengfawenzhengwen.setzhengwenAddress(file.toFilePath);
						xingzhengfawenzhengwen.setzhengwenName(file.getOldFileName());
						xingzhengfawenzhengwen.setzhengwenZhengname(file.getNewFileName());
						xingzhengfawenzhengwen.setXingzhengfawenNeirong(xingzhengfawenNeirong);
						xingzhengfawenNeirongDAO.saveZhengWen(xingzhengfawenzhengwen);
					}
				}
			}
			if (miji != null) {     
				if(b==true){			
					String[] mijiarray = xingzhengfawenNeirong.getMiji().split(",");					
					for (int i = 1; i < mijiarray.length; i++) {
						if (mijiarray[i] !=null &&!"".equals(mijiarray[i])) {
							if(mijiarray[i].equals(file.oldFileName)){
								TXingzhengfawenBeizhu xingzhengfawenBeizhu=new TXingzhengfawenBeizhu();
								xingzhengfawenBeizhu.setbeizhuAddress(file.toFilePath);
								xingzhengfawenBeizhu.setbeizhuName(file.getOldFileName());
								xingzhengfawenBeizhu.setbeizhuZhengname(file.getNewFileName());
								xingzhengfawenBeizhu.setXingzhengfawenNeirong(xingzhengfawenNeirong);
								xingzhengfawenNeirongDAO.saveBeizhu(xingzhengfawenBeizhu);
							}
						}
					}
				}else{
					if(xingzhengfawenNeirong.getMiji().equals(file.oldFileName)){
						TXingzhengfawenBeizhu xingzhengfawenBeizhu=new TXingzhengfawenBeizhu();
						xingzhengfawenBeizhu.setbeizhuAddress(file.toFilePath);
						xingzhengfawenBeizhu.setbeizhuName(file.getOldFileName());
						xingzhengfawenBeizhu.setbeizhuZhengname(file.getNewFileName());
						xingzhengfawenBeizhu.setXingzhengfawenNeirong(xingzhengfawenNeirong);
						xingzhengfawenNeirongDAO.saveBeizhu(xingzhengfawenBeizhu);
					}
				}
			}
			if(fujian!=null){
				if(c==true){			
					String[] fujianarray = xingzhengfawenNeirong.getFujian().split(",");					
					for (int i = 1; i < fujianarray.length; i++) {
						if (fujianarray[i] !=null &&!"".equals(fujianarray[i])) {
							if(fujianarray[i].equals(file.oldFileName)){
								TXingzhengfawenFujian xingzhengfawenFujian = new TXingzhengfawenFujian();
								xingzhengfawenFujian.setFujianAddress(file.toFilePath);
								xingzhengfawenFujian.setFujianName(file.getOldFileName());
								xingzhengfawenFujian.setFujianZhengname(file.getNewFileName());
								xingzhengfawenFujian.setXingzhengfawenNeirong(xingzhengfawenNeirong);
								xingzhengfawenNeirongDAO.saveFuJian(xingzhengfawenFujian);	
							}
						}
					}
				}else{
					if(xingzhengfawenNeirong.getFujian().equals(file.oldFileName)){
						TXingzhengfawenFujian xingzhengfawenFujian = new TXingzhengfawenFujian();
						xingzhengfawenFujian.setFujianAddress(file.toFilePath);
						xingzhengfawenFujian.setFujianName(file.getOldFileName());
						xingzhengfawenFujian.setFujianZhengname(file.getNewFileName());
						xingzhengfawenFujian.setXingzhengfawenNeirong(xingzhengfawenNeirong);
						xingzhengfawenNeirongDAO.saveFuJian(xingzhengfawenFujian);	
					}
				}	
			}
			
		}
		//flowTemplate.setVariable("拟稿人修改内容", xingzhengfawenNeirong.getNigaorenid().toString(), flowid);
		return id;
		
	}

	private boolean isContainChinese(String str) {
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
	}

	public TXingzhengfawenNeirong selectOneById(String id) {
		// TODO Auto-generated method stub
		return xingzhengfawenNeirongDAO.findById(id);
	}

	public Boolean updateTXingzhengfawenneirong(
			TXingzhengfawenNeirong xingzhengfawenNeirong) {
		// TODO Auto-generated method stub
		
		try {
			xingzhengfawenNeirongDAO.merge(xingzhengfawenNeirong);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List<TXingzhengfawenNeirong> findByExample(
			TXingzhengfawenNeirong xingzhengfawenNeirong) {
		// TODO Auto-generated method stub
		List<TXingzhengfawenNeirong> list = xingzhengfawenNeirongDAO.findByExample(xingzhengfawenNeirong);
		return list;
	}

	/**
	 * 根据文号类型查询最大值	 * @param 文号类型名称
	 * @return
	 */
	public Integer selectXingzhengfawenMaxNumber(String numbertype){
		DetachedCriteria criteria = DetachedCriteria.forClass(TXingzhengfawenNeirong.class);
		//	criteria.add(Restrictions.like("wenhaotype", numbertype+"〔"+(new Date().getYear() +1900)+"〕" +"%"));
			criteria.add(Restrictions.eq("wenyear", new Date().getYear() +1900));
			criteria.add(Restrictions.eq("wentype", numbertype));
			if (qbcDao.getCriteria(criteria).size() ==0) {
				return 1;
			} else {
				List<TXingzhengfawenNeirong> list = qbcDao.getCriteria(criteria);
				Collections.sort(list, new Comparator<TXingzhengfawenNeirong>(){

					public int compare(TXingzhengfawenNeirong o1,
							TXingzhengfawenNeirong o2) {
						// TODO Auto-generated method stub
						if(o1.getWenhaonumber() == null){
							return 1;
						}else if(o2.getWenhaonumber() == null){
							return 0;
						}else{
							if(Integer.parseInt(o1.getWenhaonumber()) > Integer.parseInt(o2.getWenhaonumber()) ){
								return 0;
							} else if(Integer.parseInt(o1.getWenhaonumber()) < Integer.parseInt(o2.getWenhaonumber()) ){
								return 1;
							} else {
								return 1;
							}
						}
						
					}
					
				});
				if(list.get(0) == null){
					return 1;
				}else{
					if (list.get(0).getWenhaonumber() == null) {
						return 1;
					} else {
						return Integer.parseInt(list.get(0).getWenhaonumber()) + 1;
					}
					
				}
				 
			}
		
		
	}

	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}

	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}

//	public List<TXingzhengfawenNeirong> selectXingZhengFaWenSuperDaiBanList(
//			Integer subDataBase, Integer dataBaseId, Integer userId,
//			String proname) {
//		// TODO Auto-generated method stub
//		return systemUserBo.selectXingZhengFaWenSuperDaiBanList(subDataBase,1, userId, proname);
//	}
	
//	/**
//	 * 根据用户ID查询用户上级部门发文待办
//	 * @param userId
//	 * @return
//	 */
//	public List<TTongzhifawenNeirong> selectXingZhengTongZhiSuperDaiBanList(Integer subDataBase,Integer dataBaseId, Integer userId,String proname){
//		return systemUserBo.selectTongZhiFaWenSuperDaiBanList(subDataBase,1, userId, proname);
//	}

	/**
	 * 添加行政发文内容
	 * @param 党委发文
	 * @return 党委发文ID
	 */
	public String addTXingzhengfawenneirong(TXingzhengfawenNeirong xingzhengfawenNeirong){
		xingzhengfawenNeirongDAO.save(xingzhengfawenNeirong);
		return xingzhengfawenNeirong.getId();
	}

	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}

	public List<TaoHongMoBan> selectTaoHongMoBan(TaoHongMoBan taoHongMoBan) {
		// TODO Auto-generated method stub
		String sql = new String("SELECT * FROM  dbo.t_system_taohongmuban WHERE flowtype = ? AND state = ? ");
		List<TaoHongMoBan> list =  springDao.querySQL(sql, new Object[]{taoHongMoBan.getFlowType(),taoHongMoBan.getState()}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TaoHongMoBan hongMoBan = new TaoHongMoBan();
				hongMoBan.setFileName(arg0.getString("fileName"));
				hongMoBan.setViewName(arg0.getString("viewName"));
				return hongMoBan;
			}
			
		});
		return list;
	}

	public List<YinZhang> selectYinZhang(YinZhang yinZhang) {
		// TODO Auto-generated method stub
		String sql = new String("SELECT * FROM  dbo.t_system_yinzhanglist WHERE flowtype = ? AND state = ? ");
		List<YinZhang> list =  springDao.querySQL(sql, new Object[]{yinZhang.getFlowType(),yinZhang.getState()}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				YinZhang hongMoBan = new YinZhang();
				hongMoBan.setFileName(arg0.getString("fileName"));
				hongMoBan.setViewName(arg0.getString("viewName"));
				return hongMoBan;
			}
			
		});
		return list;
	}
	
	/**
	 * 文件查询
	 * @param userid 用户ID
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wenyear 年度
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
	public List<FaWenInfo> selectChaYueWenJian(Integer userid, Integer first,Integer max,String biaoti,String wenyear,String wentype,String zhuangtai,String wenhao,String maintypeid){
		String prename = "";
		if ("1".equals(maintypeid)||"6".equals(maintypeid)||"7".equals(maintypeid)){
			prename="chayuewenjianxz";
		}else{
			prename="chayuewenjiandw";
		}
		
		String sql =  new String("EXEC "+prename+" @first = ?,@limit = ?,@userid = ?,@useridlike = ?,@biaotiselect = ?,@wenyear = ?,@wentype = ?,@zhuangtai = ?,@wenhao = ? ");
		List<FaWenInfo> fawenList = springDao.querySQL(sql, new Object[]{first,max,userid,"%,"+userid+",%",biaoti,wenyear,wentype,zhuangtai,wenhao}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				if(arg0.getString("id") != null){
					FaWenInfo faWenInfo = new FaWenInfo();
					faWenInfo.setBiaoti(arg0.getString("biaoti"));
					faWenInfo.setWenyear(arg0.getString("wenyear"));
					faWenInfo.setWenhaotype(arg0.getString("wenhaotype"));
					faWenInfo.setFawendatetime(arg0.getDate("fawendatetime"));
					faWenInfo.setWentype(arg0.getString("wentype"));
					faWenInfo.setZhuangtai(arg0.getString("zhuangtai"));
					faWenInfo.setFlowId(arg0.getInt("flowid"));
					faWenInfo.setId(arg0.getString("id"));
					faWenInfo.setTableName(arg0.getString("tablename"));
					faWenInfo.setWenhao(arg0.getInt("wenhao"));
					return faWenInfo;
				}else{
					FaWenInfo faWenInfo = new FaWenInfo();
					faWenInfo.setCount(arg0.getInt("count"));
					return faWenInfo;
				}
				
			}
			
		});
		
		return fawenList;
	}
	
	/**
	 * 文件查询
	 * @param userid 用户ID
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wenyear 年度
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
	public Integer selectChaYueWenJianCount(Integer userid,String biaoti,String wenyear,String wentype,String zhuangtai,String wenhao,String maintypeid){
		String prename = "";
		if ("1".equals(maintypeid)||"6".equals(maintypeid)){
			prename="chayuewenjiancountxz";
		}else{
			prename="chayuewenjiancountdw";
		}
		String sql =  new String("EXEC "+prename+" @userid = ?,@useridlike = ?,@biaotiselect = ?,@wenyear = ?,@wentype = ?,@zhuangtai = ?,@wenhao = ? ");
		List<Integer> countList = springDao.querySQL(sql, new Object[]{userid,"%,"+userid+",%",biaoti,wenyear,wentype,zhuangtai,wenhao}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("count");
				
			}
			
		});
		
		return countList.get(0);
	}
	
	/**
	 * 行政流程跟踪
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
	public List<FaWenInfo> xingZhengFlowGengZong(Integer first,Integer max,String biaoti,String wentype,String zhuangtai,String fawenwenhao,String starttime,String endtime,String zhuangta){
		String sql =  new String("EXEC xingzhenggenzhong @first = ?,@limit = ?,@biaotiselect = ?,@wentype = ?,@zhuangtai = ?,@fawenwenhao= ?,@starttime = ?,@endtime = ?,@zhuangta = ?");
		List<FaWenInfo> fawenList = springDao.querySQL(sql, new Object[]{first,max,biaoti,wentype,zhuangtai,fawenwenhao,starttime,endtime,zhuangta}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				FaWenInfo faWenInfo = new FaWenInfo();
				faWenInfo.setWentype(arg0.getString("wentype"));
				faWenInfo.setBiaoti(arg0.getString("biaoti"));
				faWenInfo.setPeople(arg0.getString("username"));
				faWenInfo.setZhuangtai(arg0.getString("zhuangtai"));																
				faWenInfo.setFlowId(arg0.getInt("flowid"));
				faWenInfo.setId(arg0.getString("id"));
				faWenInfo.setTableName(arg0.getString("tablename"));
				faWenInfo.setEndDatetime(arg0.getDate("taskend"));
				faWenInfo.setFawenwenhao(arg0.getString("fawenwenhao"));
				faWenInfo.setStarttime(arg0.getString("starttime"));
				faWenInfo.setEndtime(arg0.getString("endtime"));
				faWenInfo.setZhuangta(arg0.getString("zhuangta"));
				return faWenInfo;
				
			}
			
		});
		
		return fawenList;
	}
	
	/**
	 * 行政流程跟踪行数
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
	public Integer xingZhengFlowGengZongCount(Integer first,Integer max,String biaoti,String wentype,String zhuangtai,String fawenwenhao,String starttime,String endtime,String zhuangta){
		String sql =  new String("EXEC xingzhenggenzhongcount @first = ?,@limit = ?,@biaotiselect = ?,@wentype = ?,@zhuangtai = ?,@fawenwenhao = ?,@starttime = ?,@endtime = ?,@zhuangta = ?");
		List<Integer> countList = springDao.querySQL(sql, new Object[]{first,max,biaoti,wentype,zhuangtai,fawenwenhao,starttime,endtime,zhuangta}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("count");
				
			}
			
		});
		
		return countList.get(0);
	}
	
	/**
	 * 党委流程跟踪
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
	public List<FaWenInfo> dangweiFlowGengZong(Integer first,Integer max,String biaoti,String wentype,String zhuangtai,String fawenwenhao,String starttime,String endtime,String zhuangta){
		String sql =  new String("EXEC dangweigenzhong @first = ?,@limit = ?,@biaotiselect = ?,@wentype = ?,@zhuangtai = ?,@fawenwenhao = ?,@starttime = ?,@endtime = ?,@zhuangta = ?");
		List<FaWenInfo> fawenList = springDao.querySQL(sql, new Object[]{first,max,biaoti,wentype,zhuangtai,fawenwenhao,starttime,endtime,zhuangta}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				FaWenInfo faWenInfo = new FaWenInfo();
				faWenInfo.setWentype(arg0.getString("wentype"));
				faWenInfo.setBiaoti(arg0.getString("biaoti"));
				faWenInfo.setPeople(arg0.getString("username"));
				faWenInfo.setZhuangtai(arg0.getString("zhuangtai"));																
				faWenInfo.setFlowId(arg0.getInt("flowid"));
				faWenInfo.setId(arg0.getString("id"));
				faWenInfo.setTableName(arg0.getString("tablename"));
				faWenInfo.setEndDatetime(arg0.getDate("taskend"));
				faWenInfo.setFawenwenhao(arg0.getString("fawenwenhao"));
				faWenInfo.setStarttime(arg0.getString("starttime"));
				faWenInfo.setEndtime(arg0.getString("endtime"));
				faWenInfo.setZhuangta(arg0.getString("zhuangta"));
				return faWenInfo;
				
			}
			
		});
		
		return fawenList;
	}
	
	/**
	 * 党委流程跟踪行数
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
	public Integer dangweiFlowGengZongCount(Integer first,Integer max,String biaoti,String wentype,String zhuangtai,String fawenwenhao,String starttime,String endtime,String zhuangta){
		String sql =  new String("EXEC dangweigenzhongcount @first = ?,@limit = ?,@biaotiselect = ?,@wentype = ?,@zhuangtai = ?,@fawenwenhao = ?,@starttime = ?,@endtime = ?,@zhuangta = ?");
		List<Integer> countList = springDao.querySQL(sql, new Object[]{first,max,biaoti,wentype,zhuangtai,fawenwenhao,starttime,endtime,zhuangta}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("count");
				
			}
			
		});
		
		return countList.get(0);
	}
	
	/**
	 * 根据ID查询行政发文
	 * @param id
	 * @return
	 */
	public Boolean deleteOneById(String id){
		TXingzhengfawenNeirong xingzhengfawenNeirong =  selectOneById(id);
		xingzhengfawenNeirongDAO.delete(xingzhengfawenNeirong);
		return true;
	}
	
	
	public Integer wenyearmax(){
		Integer year = (new Date().getYear() + 1900);
		StringBuffer sql = new StringBuffer("SELECT MAX(table_.wenyear) AS wenyear FROM ( ");
		sql.append("SELECT MAX(wenyear) AS wenyear FROM dbo.t_xingzhengfawen_neirong AS xingzhengfawen  ");
		sql.append("UNION ");
		sql.append("SELECT MAX(wenyear) AS wenyear FROM dbo.t_dangweifawen_neirong AS dangweifawen  ");
//		sql.append("UNION ");
//		sql.append("SELECT MAX(wenyear) AS wenyear FROM dbo.t_tongzhifawen_neirong AS tongzhifawen  ");
		sql.append(") AS table_ GROUP BY table_.wenyear HAVING  table_.wenyear IS NOT NULL ");
		List<Integer> list =  springDao.querySQL(sql.toString(), new Object[]{}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getObject("wenyear");
			}
			
		});
		if (list.size() > 0 ) {
			year = list.get(0);
		} else {
			;
		}
		return year;
	}
	
	public Integer wenyearmin(){
		Integer year = (new Date().getYear() + 1900);
		StringBuffer sql = new StringBuffer("SELECT MIN(table_.wenyear) AS wenyear FROM ( ");
		sql.append("SELECT MIN(wenyear) AS wenyear FROM dbo.t_xingzhengfawen_neirong AS xingzhengfawen  ");
		sql.append("UNION ");
		sql.append("SELECT MIN(wenyear) AS wenyear FROM dbo.t_dangweifawen_neirong AS dangweifawen  ");
		sql.append(") AS table_ GROUP BY table_.wenyear HAVING  table_.wenyear IS NOT NULL ");
		List<Integer> list =  springDao.querySQL(sql.toString(), new Object[]{}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getObject("wenyear");
			}
			
		});
		if (list.size() > 0 ) {
			year = list.get(0);
		} else {
			;
		}
		return year;
	}
	
	public String dangQianHuanJie(Integer flowId){
		StringBuffer sql = new StringBuffer("SELECT node.NAME_ AS huanjie   FROM dbo.JBPM_TOKEN AS token JOIN ");
		sql.append("dbo.JBPM_NODE AS node ON node.ID_ = token.NODE_  ");
		sql.append("WHERE token.ID_ = ? ");
		return  (String) springDao.querySQL(sql.toString(), new Object[]{flowId}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getObject("huanjie");
			}
			
		}).get(0);
	}
	
	public List<TSystemUser> selectTsystemUserByUserIdAndDuty(Integer userid,String DutyName){
		List<TSystemUser> list = new ArrayList<TSystemUser>();
		StringBuffer sql = new StringBuffer("SELECT usermain.userId,usermain.userName FROM dbo.t_system_user AS usermain JOIN ");
		sql.append("dbo.t_system_userDepartment AS userDepartment ON userDepartment.userId = usermain.userId JOIN ");
		sql.append("dbo.t_system_userDuty AS userDuty ON userDuty.userID = usermain.userId JOIN ");
		sql.append("dbo.t_system_duty AS duty ON duty.dutyID = userDuty.dutyID ");
		sql.append("WHERE userDepartment.departmentId IN (SELECT userDepartment.departmentId AS departmentId FROM dbo.t_system_user AS systemuer JOIN ");
		sql.append("dbo.t_system_userDepartment AS userDepartment ON userDepartment.userId = systemuer.userId ");
		sql.append("WHERE systemuer.userId = ?) AND duty.dutyName = ? and usermain.state=0");
		list = springDao.querySQL(sql.toString(),  new Object[]{userid,DutyName}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TSystemUser systemUser = new TSystemUser();
				systemUser.setUserId(arg0.getInt("userId"));
				systemUser.setUserName(arg0.getString("userName"));
				return systemUser;
			}
			
		});
		return list;
	}
	
	public List<TSystemUser> selectTsystemUserByUserIdAndDuty(String DutyName){
		List<TSystemUser> list = new ArrayList<TSystemUser>();
		StringBuffer sql = new StringBuffer("SELECT DISTINCT  usermain.userId,usermain.userName,usermain.orderNo FROM dbo.t_system_user AS usermain JOIN ");
		sql.append("dbo.t_system_userDepartment AS userDepartment ON userDepartment.userId = usermain.userId JOIN ");
		sql.append("dbo.t_system_userDuty AS userDuty ON userDuty.userID = usermain.userId JOIN ");
		sql.append("dbo.t_system_duty AS duty ON duty.dutyID = userDuty.dutyID ");
		sql.append("WHERE duty.dutyName = ? and usermain.state=0 ORDER BY orderNo");
	
		list = springDao.querySQL(sql.toString(),  new Object[]{DutyName}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TSystemUser systemUser = new TSystemUser();
				systemUser.setUserId(arg0.getInt("userId"));
				systemUser.setUserName(arg0.getString("userName"));
				return systemUser;
			}
			
		});
		return list;
	}
	
	public List<TSystemUser> selectTsystemUserByDemIdAndDuty(String demId,String DutyName){
		List<TSystemUser> list = new ArrayList<TSystemUser>();

		StringBuffer sql = new StringBuffer("SELECT usermain.userId,usermain.userName FROM dbo.t_system_user AS usermain JOIN ");
		sql.append("dbo.t_system_userDepartment AS userDepartment ON userDepartment.userId = usermain.userId JOIN ");
		sql.append("dbo.t_system_userDuty AS userDuty ON userDuty.userID = usermain.userId JOIN ");
		sql.append("dbo.t_system_duty AS duty ON duty.dutyID = userDuty.dutyID ");
		sql.append("WHERE userDepartment.departmentId = ? AND duty.dutyName = ? and usermain.state=0");
		list = springDao.querySQL(sql.toString(),  new Object[]{demId,DutyName}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TSystemUser systemUser = new TSystemUser();
				systemUser.setUserId(arg0.getInt("userId"));
				systemUser.setUserName(arg0.getString("userName"));
				return systemUser;
			}
			
		});
		return list;
		
	}
	
	/**
	 * 团委流程跟踪
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
//	public List<FaWenInfo> tuanweiFlowGengZong(Integer first,Integer max,String biaoti,String wentype,String zhuangtai,Integer state){
//		String sql =  new String("EXEC tuanweigenzhong @first = ?,@limit = ?,@state = ?,@biaotiselect = ?,@wentype = ?,@zhuangtai = ?");
//		List<FaWenInfo> fawenList = springDao.querySQL(sql, new Object[]{first,max,state,biaoti,wentype,zhuangtai}, new RowMapper(){
//
//			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
//				// TODO Auto-generated method stub
//				FaWenInfo faWenInfo = new FaWenInfo();
//				faWenInfo.setWentype(arg0.getString("wentype"));
//				faWenInfo.setBiaoti(arg0.getString("biaoti"));
//				faWenInfo.setPeople(arg0.getString("username"));
//				faWenInfo.setZhuangtai(arg0.getString("zhuangtai"));																
//				faWenInfo.setFlowId(arg0.getInt("flowid"));
//				faWenInfo.setId(arg0.getString("id"));
//				faWenInfo.setTableName(arg0.getString("tablename"));
//				return faWenInfo;
//				
//			}
//			
//		});
//		
//		return fawenList;
//	}
	
	/**
	 * 团委流程跟踪行数
	 * @param first	 起始行
	 * @param max    行数
	 * @param biaoti 标题
	 * @param wentype 文件类型
	 * @param zhuangtai 状态
	 * @return
	 */
//	public Integer tuanweiFlowGengZongCount(Integer first,Integer max,String biaoti,String wentype,String zhuangtai,Integer state){
//		String sql =  new String("EXEC tuanweigenzhongcount @state = ?,@biaotiselect = ?,@wentype = ?,@zhuangtai = ?");
//		List<Integer> countList = springDao.querySQL(sql, new Object[]{state,biaoti,wentype,zhuangtai}, new RowMapper(){
//
//			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
//				// TODO Auto-generated method stub
//				return arg0.getInt("count");
//				
//			}
//			
//		});
//		
//		return countList.get(0);
//	}
	
	public Integer selectTaskCont(Integer userId,String node,String proname){
		StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS counttask FROM dbo.JBPM_TASKINSTANCE AS taskinstance JOIN dbo.JBPM_PROCESSINSTANCE AS processinstance ON ");
		sql.append("taskinstance.PROCINST_ = processinstance.ID_ JOIN dbo.JBPM_PROCESSDEFINITION AS processdefinition ON ");
		sql.append("processdefinition.ID_ = processinstance.PROCESSDEFINITION_ ");
		sql.append("WHERE taskinstance.ACTORID_ = ? AND taskinstance.ISOPEN_ = 1 AND taskinstance.ISSIGNALLING_ = 1 AND taskinstance.ISSUSPENDED_ = 0  AND processdefinition.NAME_ = ? ");
		sql.append("AND taskinstance.NAME_ = ?  ");
		sql.append("AND ((SELECT COUNT(*) FROM dbo.t_xingzhengfawen_neirong AS xingzhengfawen WHERE xingzhengfawen.flowId = processinstance.ID_) > 0 OR (SELECT COUNT(*) FROM dbo.t_dangweifawen_neirong AS dangweifawen WHERE dangweifawen.flowId = processinstance.ID_) > 0 )  ");
		List<Integer> countList = springDao.querySQL(sql.toString(), new Object[]{userId,proname,node}, new RowMapper(){

			
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("counttask");
				
			}
			
		});
		
		return countList.get(0);
	}
	
	public List<TaskInstance> selectTaskInstanceByUserIdAndProcessName(Integer userId,String processname){
		StringBuffer sql = new StringBuffer("SELECT taskinstance.ID_ ,processinstance.ID_ AS processid,taskinstance.DESCRIPTION_  FROM dbo.JBPM_TASKINSTANCE  AS taskinstance JOIN dbo.JBPM_PROCESSINSTANCE AS processinstance ON  taskinstance.PROCINST_ = processinstance.ID_  ");
		sql.append("JOIN dbo.JBPM_PROCESSDEFINITION AS process ON process.ID_ = processinstance.PROCESSDEFINITION_  ");
		sql.append("WHERE taskinstance.ACTORID_ = ? AND process.NAME_ = ? AND taskinstance.ISOPEN_ = 1 AND taskinstance.ISSIGNALLING_ = 1  AND taskinstance.ISSUSPENDED_ <> 1 ORDER BY taskinstance.ID_ desc");
		List<TaskInstance > taskInstances = 	springDao.querySQL(sql.toString(), new Object[]{userId,processname}, new RowMapper() {
			
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TaskInstance taskInstance = new TaskInstance();
				taskInstance.setId(arg0.getLong("ID_"));
				taskInstance.setPriority(arg0.getInt("processid"));
				taskInstance.setDescription(arg0.getString("DESCRIPTION_"));
				return taskInstance;
			}
		});
		
		return taskInstances;
	}
	
	public Boolean deleteTask(Integer taskId){
		try {
			StringBuffer sql = new StringBuffer("delete from  dbo.JBPM_LOG WHERE TASKINSTANCE_ = ?  ");
			springDao.commonUpdate(sql.toString(), new Object[]{taskId});
			 sql = new StringBuffer("delete from JBPM_TASKINSTANCE where ID_ = ? ");
			springDao.commonUpdate(sql.toString(), new Object[]{taskId});
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
}
